﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
//using SFsoftSite.Membership;
using System.Web.Security;
using SFsoftSite.Memberships;
using SFsoftSite.Profiles;

namespace SFsoftSite.Cadastros
{
    public partial class CadastroCliente : System.Web.UI.Page
    {
        #region Atributos

        BLCliente _cliente;

        #endregion

        #region Page Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (HttpContext.Current.User.Identity.Name != string.Empty)
                {
                    Response.Redirect("~/Jogos/ListarJogos.aspx");
                }
            }
        }

        #endregion

        #region Eventos

        protected void btnCadastrar_Click(object sender, EventArgs e)
        {
            Page.Validate();

            if (Page.IsValid)
            {
                SFUserManager user = (SFUserManager)Membership.Provider;
                MembershipCreateStatus status;

                try
                {
                    user.CreateUser(txtNome.Text, txtEmail.Text, txtSenha.Text, out status);

                    if (status == MembershipCreateStatus.Success)
                    {
                        SFUserInfo us = (SFUserInfo)user.GetUser(txtEmail.Text, false);

                        SFProfile profile = SFProfile.GetProfile();
                        profile.Cliente.ID = us.ID;
                        profile.Cliente.Nome = us.Nome;
                        profile.Cliente.Email = us.Email;
                        profile.Cliente.Senha = us.Senha;
                        profile.Cliente.Foto = us.Foto;
                        profile.Cliente.Endereco = us.Endereco;
                        profile.Cliente.Telefone = us.Telefone;
                        profile.Save();

                        FormsAuthentication.SetAuthCookie(txtEmail.Text, true);

                        Roles.AddUserToRole(txtEmail.Text, "Cliente");

                        FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, false);
                    }
                }
                catch
                {
                    string mensagem = "Já existe um usuário com esse e-mail!";

                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Mensagem", "alert(' " + mensagem + "')", true);
                }
            }
        }

        #endregion
    }
}